package com.ruoyi.edu.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.edu.domain.vo.EduSubjectVo;
import com.ruoyi.edu.domain.bo.EduSubjectBo;
import com.ruoyi.edu.service.IEduSubjectService;

/**
 * 课程科目
 *
 * @author ruoyi
 * @date 2022-08-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/edu/subject")
public class EduSubjectController extends BaseController {

    private final IEduSubjectService iEduSubjectService;

    @PostMapping("/listChildren/{id}")
    public R listChildren(@PathVariable String id){
       List<EduSubjectVo> list= iEduSubjectService.queryByFId(id);
       return R.ok(list);

    }
    /**
     * 查询课程科目列表
     */
    @SaCheckPermission("edu:subject:list")
    @GetMapping("/list")
    public R<List<EduSubjectVo>> list(EduSubjectBo bo) {
        List<EduSubjectVo> list = iEduSubjectService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出课程科目列表
     */
    @SaCheckPermission("edu:subject:export")
    @Log(title = "课程科目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EduSubjectBo bo, HttpServletResponse response) {
        List<EduSubjectVo> list = iEduSubjectService.queryList(bo);
        ExcelUtil.exportExcel(list, "课程科目", EduSubjectVo.class, response);
    }

    /**
     * 获取课程科目详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("edu:subject:query")
    @GetMapping("/{id}")
    public R<EduSubjectVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iEduSubjectService.queryById(id));
    }

    /**
     * 新增课程科目
     */
    @SaCheckPermission("edu:subject:add")
    @Log(title = "课程科目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EduSubjectBo bo) throws Exception {
        return toAjax(iEduSubjectService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改课程科目
     */
    @SaCheckPermission("edu:subject:edit")
    @Log(title = "课程科目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EduSubjectBo bo) throws Exception {
        return toAjax(iEduSubjectService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除课程科目
     *
     * @param ids 主键串
     */
    @SaCheckPermission("edu:subject:remove")
    @Log(title = "课程科目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) throws Exception {
        return toAjax(iEduSubjectService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
